import { createRouter, createWebHistory } from 'vue-router'

const routes = [
  {
    path: '/index',
    component: () => import('@/views/IndexView.vue'),
    children: [
      {
        path: '/overview',
        component: () => import('@/views/OverView.vue'),
      },
      {
        path: '/localfile',
        component: () => import('@/views/FileView.vue'),
      },
      {
        path: '/article',
        component: () => import('@/views/ArticleView.vue'),
      },
      {
        path: '/gallery',
        component: () => import('@/views/GalleryView.vue'),
      },
      {
        path: '/diary',
        component: () => import('@/views/DiaryView.vue'),
      },
      {
        path: '/install',
        component: () => import('@/views/InstallView.vue'),
      },
    ],
  },
  {
    path: '/editgallery',
    component: () => import('@/views/EditGallery.vue'),
  },
  {
    path: '/editarticle',
    component: () => import('@/views/EditArticleView.vue'),
  },
  {
    path: '/login',
    component: () => import('@/views/LoginView.vue'),
  },
  {
    path: '/register',
    component: () => import('@/views/RegisterView.vue'),
  },
  {
    path: '/introduce',
    component: () => import('@/views/IntroduceView.vue'),
  },
  {
    path: '/',
    redirect: '/introduce',
  },
]
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes,
})

router.beforeEach((to, from, next) => {
  if (to.matched.length === 0) {
    // 地址错误发生跳转
    if (from.name) {
      next({
        name: from.name,
      })
    } else {
      next('/')
    }
  } else {
    next()
  }
})

export default router
